#define _PCF8574_C_

#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
#include <util/delay.h>

#include "config.h"
#include "typedefs.h"
#include "menuGen.h"
#include "TIMEBASE.H"
#include "t6963c.h"
#include "buttons.h"

#include "i2cmaster.h"

#define C_PCF8574_DEVICE_ADDRESS 0x70

unsigned char ucPcf8574PortState = 0xFF; // All ports are set to Input by default!


unsigned char pcf8574SetPin(unsigned char ucPin, unsigned char ucState){

  if (ucPin > 7 || ucState > 1) return 1; // not possible
  if (ucState == 1){
    ucPcf8574PortState |= (1<<(ucPin));
  } else {
    ucPcf8574PortState &= ~(1<<(ucPin));
  }
  i2c_start(C_PCF8574_DEVICE_ADDRESS);
  i2c_write(ucPcf8574PortState);
  i2c_stop();

  return 0;

}

unsigned char pcf8574SetPort(unsigned char ucValue){

  i2c_start(C_PCF8574_DEVICE_ADDRESS);
  i2c_write(ucValue);
  i2c_stop();

  ucPcf8574PortState = ucValue;

  return 0;

}
